package com.hzhr.iot.device.controller;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.Author;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.device.controller.base.BaseController;
import com.hzhr.iot.device.model.flowMeter.*;
import com.hzhr.iot.device.service.FlowMeterService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

@Api(tags = "FlowMeter", description = "流量计")
@RestController
@RequestMapping(value = "")
public class FlowMeterController extends BaseController {
    @Autowired
    FlowMeterService flowMeterService;

    //region /flow-meters POST
    //region swagger响应码 : post
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功"),
    })
    //endregion
    //region swagger说明 : post
    @ApiOperation(value="创建流量计", notes="创建流量计  \n简介 : 物联网设备公司现场实施人员创建流量计和DTU，并将该流量计和相应的DTU及燃气用户进行关联  \n步骤 :  \n1. 查询燃气用户：根据请求体“流量计模型(新增)”的“流量计代码”和“所属部门编号”查询燃气用户“燃气用户_1”，  \n 如果“燃气用户_1”不存在，抛出异常“燃气用户不存在”；(√)  \n2. 查询DTU：根据“流量计模型(新增)”的“DTU代码”查询DTU“DTU_1”；(√)  \n3. 查询流量计：根据“流量计模型(新增)”的“流量计代码”和“所属公司编号”查询流量计“流量计_1”；(√)  \n4. 判断流量计和DTU的存在性：如果“DTU_1”和“流量计_1”都存在，直接返回“DTU_1”和“流量计_1”的编号，  \n 如果“DTU_1”存在且“流量计_1”不存在，抛出异常“DTU代码已存在”，  \n 如果“流量计_1”存在且“DTU_1”不存在，抛出异常“流量计代码已存在”；(√)  \n5. 创建DTU：根据“流量计模型(新增)”的信息在登录用户所在的物联网设备公司下创建DTU“DTU_2”；(√)  \n6. 创建流量计：根据“流量计模型(新增)”的信息以及需要关联“DTU_2”和“燃气用户_1”来创建流量计“流量计_2”；(√)  \n8. 返回信息：根据“流量计_2”和“DTU_2”生成“流量计响应模型(新增)”并返回；(√)  \n",produces = "application/json")
    //endregion
    //region swagger参数 : post
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),
    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),
    })
    //endregion
    //region 请求 : post
    @RequestMapping(value = "/flow-meters", method = RequestMethod.POST)
    //endregion
    @Author
    public ResponseEntity<FlowMeterPostResponseModel> post(@RequestParam @ApiIgnore Map<String, Object> query,
                                                           @RequestBody FlowMeterPostModel body) throws ServiceException {
        FlowMeterPostResponseModel responseBody = flowMeterService.post(initHeader(), query, body);
        return new ResponseEntity<>(responseBody, HttpStatus.OK);
        //endregion
    }
    //endregion
    //region /flow-meters DELETE
    //region swagger响应码 : delete
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功"),
    })
    //endregion
    //region swagger说明 : delete
    @ApiOperation(value="删除流量计", notes="删除流量计  \n简介 : 物联网设备公司现场实施人员删除流量计和所属的DTU  \n步骤 :  \n1. 查询流量计：根据流量计编号查询“流量计_1”，如“流量计_1”不存在，抛出异常“流量计不存在”; (√)  \n2. 查询DTU：根据“流量计_1”的DTU编号查询“DTU_1”，如果“DTU_1”不存在，抛出异常“DTU不存在”; (√)  \n3. 删除流量计：删除“流量计_1”; (√)  \n4. 删除DTU：删除“DTU_1”; (√)  \n",produces = "application/json")
    //endregion
    //region swagger参数 : delete
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),
    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),
    })
    //endregion
    //region 请求 : delete
    @RequestMapping(value = "/flow-meters", method = RequestMethod.DELETE)
    //endregion
    @Author
    public ResponseEntity<FlowMeterDeleteResponseModel> delete(@RequestParam @ApiIgnore Map<String, Object> query,
                                                               @RequestBody FlowMeterDeleteModel body) throws ServiceException {
        FlowMeterDeleteResponseModel responseBody = flowMeterService.delete(initHeader(), query, body);
        return new ResponseEntity<>(responseBody, HttpStatus.OK);
        //endregion
    }
    //endregion
    //region /flow-meters PUT
    //region swagger响应码 : put
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功"),
    })
    //endregion
    //region swagger说明 : put
    @ApiOperation(value="修改流量计", notes="修改流量计  \n简介 : 物联网设备公司现场实施人员修改流量计和所属DTU的信息  \n步骤 :  \n1. 查询流量计：根据请求体“流量计模型(修改)”的“流量计编号”查询流量计“流量计_1”，  \n 如果“流量计_1”不存在，抛出异常“流量计不存在”；(√)  \n2. 查询DTU：根据“流量计_1”的“所属物联网设备编号”查询“DTU_1”，  \n 如果“DTU_1”不存在，抛出异常“DTU不存在”；(√)  \n3. 验证新的流量计代码：如果“流量计模型(修改)”的“流量计代码”和“流量计_1”的“代码”不同，  \n 则验证“流量计模型(修改)”的“流量计代码”在“流量计模型(修改)”的“所属公司编号”所对应的公司下是否存在，  \n 如果存在，抛出异常“流量计代码已存在”；(√)  \n4. 验证新的DTU代码：如果“流量计模型(修改)”的“DTU代码”和“DTU_1”的“代码”不同，  \n 则“流量计模型(修改)”的“DTU代码”是否存在，如果存在，抛出异常“DTU代码已存在”；(√)  \n5. 验证新的所属燃气用户：如果“流量计模型(修改)”的“所属燃气用户代码”和“流量计_1”的“所属燃气用户代码”不同，  \n 则根据“流量计模型(修改)”的“所属燃气用户代码”查询“流量计模型(修改)”的“所属部门编号”所对应的部门的管辖范围内的燃气用户“燃气用户_1”，  \n 如果“燃气用户_1”不存在，抛出异常“燃气用户不存在”；(√)  \n6. 更新流量计及DTU信息：根据“流量计模型(修改)”更新“流量计_1”和“DTU_1”的相关信息，  \n 如果“流量计模型(修改)”的“燃气用户代码”和“流量计_1”的燃气用户代码不同，则需关联“流量计_1”和“燃气用户_1”; (√)  \n7. 返回信息：生成“流量计响应模型(修改)”并返回；(√)  \n",produces = "application/json")
    //endregion
    //region swagger参数 : put
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),
    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),
    })
    //endregion
    //region 请求 : put
    @RequestMapping(value = "/flow-meters", method = RequestMethod.PUT)
    //endregion
    @Author
    public ResponseEntity<FlowMeterPutResponseModel> put(@RequestParam @ApiIgnore Map<String, Object> query,
                                                             @RequestBody FlowMeterPutModel body) throws ServiceException {
        FlowMeterPutResponseModel responseBody = flowMeterService.put(initHeader(), query, body);
        return new ResponseEntity<>(responseBody, HttpStatus.OK);
        //endregion
    }
    //endregion
}

















































































































































































































































































































































































































































































































































































































































































